<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.yaodian100.core.common.utils.ServerValue"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ include file="/taglibs.jsp"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> 	
	<!-- 验证 -->
	<script type="text/javascript" charset="UTF-8" src="/js/formValidator_min.js" charset="UTF-8"></script>
	<script type="text/javascript" charset="UTF-8" src="/js/formValidator.js" charset="UTF-8"></script>
	<script type="text/javascript" charset="UTF-8" src="/js/formValidatorRegex.js" charset="UTF-8"></script>
	<!-- 数字验证 -->
	<script type="text/javascript" charset="UTF-8" src="/js/jquery.alphanumeric.js"></script>
			<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick.js"></script>
<script type="text/javascript" charset="UTF-8" src="/js/jquery.datepick-zh-CN.js"></script>
<link rel="stylesheet" type="text/css" href="/css/smoothness.datepick.css"/>
<link rel="stylesheet" href="/css/jquery.tooltip.css" />
<script src="/js/jquery.tooltip.min.js" type="text/javascript"></script>
	<script language="javascript"><!--
	$().ready( function() {
		$("tr[id*='tdDesc_']").tooltip();
			//$.formValidator.initConfig({formid:"specForm",onerror:function(msg){alert(msg)},onsuccess:function(){return true;}});
			//$("#spcName").formValidator({onfocus:"請輸入規格名稱",oncorrect:"規格名稱輸入正確"}).inputValidator({min:2,max:100,onerror:"規格名稱须介于2-100个字元之间且不能为空值"});
			$('#arrivalDate').datepick({minDate: '+1D', showOn: 'both',numberOfMonths:2,
	    	buttonImageOnly: true, buttonImage: '/images/calendar.gif',dateFormat:"yy-mm-dd",yearRange: '-1:+1'});
	    	
			$("input[name='spcReverseInventory']").each(function(i,item){
				$(this).numeric();
			});
			$("input[name='spcMaxCountPerOrder']").each(function(i,item){
				$(this).numeric();
			});
			$("input[name='spcMinCountPerOrder']").each(function(i,item){
				$(this).numeric();
			});
			$("input[name='spcSafeInventory']").each(function(i,item){
				$(this).numeric();
			});
			$("input[name='showSpecSeq']").each(function(i,item){
				$(this).numeric();
			});
			$("input[name='spcHotSaleInventory']").each(function(i,item){
				$(this).numeric();
			});
			
			$("input[id*='spcName_']").each(function(i,item){					
				if(item.value != ""){
					$(this).attr("readonly",true);
				}
			});

			$("#submitStock").click(function(){
				if ( "" == $("#arrivalDate").val() ){
					alert("请输入预计到货日");
					return false;
				}
				$("#specForm").attr("action","updateArrivalDate.do");
				$("#specForm").submit();
			});
			/*
			$("input[id*='spcHotSaleInventory_']").blur(function(){
				sumInv();
			});
			*/
			$("#submit_button").click(function(){
				try {
					$("input[id*='spcName_']").each(function(i,item){					
						if(item.value == ""){
							alert("请输入规格名称");
							item.focus();
							throw item;
						}
					});
					$("input[id*='spcReverseInventory_']").each(function(i,item){
						if(item.value == ""){
							alert("请输入库存保留量");
							item.focus();
							throw item;
						}
						
					});
								
					$("input[id*='spcMaxCountPerOrder_']").each(function(i,item){
						if(item.value == ""){
							alert("请输入订单,单笔最大可销售数");
							item.focus();
							throw item;
						}
						if(parseInt(item.value)<parseInt($("input[id='spcMinCountPerOrder_"+(i+1)+"']").val())){
							alert("单笔最大可销售数不可小於 单笔最小可销售数");
							item.focus();
							throw item;
						}
					});
					$("input[id*='spcMinCountPerOrder_']").each(function(i,item){
						if(item.value == ""){
							alert("请输入订单,单笔最小可销售数");
							item.focus();
							throw item;
						}
						if(parseInt(item.value)<=parseInt(0)){
							alert("单笔最小可销售数要大於0");
							item.focus();
							throw item;
						}
					});
				
					$("input[id*='spcSafeInventory_']").each(function(i,item){
						if(item.value == ""){
							alert("请输入安全库存量");
							item.focus();
							throw item;
						}
					});		
					$("input[id*='spcHotSaleInventory_']").each(function(i,item){
						if ( item.value == "" ){
							alert("热销 数量要大於等于0");
							item.focus();
							throw item;
						}
					});
					$("input[id*='showSeq_']").each(function(i,item){
						if(item.value == ""){
							alert("请输入排序【0～99】");
							item.focus();
							throw item;
						}
					});
				}catch(e){
					return false;
				}

				if ($("#spcName").val() == "" && ($("#spcReverseInventory").val() != ""  || $("#spcMaxCountPerOrder").val() != "" ||  $("#spcMinCountPerOrder").val() != "")){
					alert("请输入规格名称");
					$("#spcName").focus();
					return false;
					
				}
				if ($("#spcName").val() != "" ){
					if ($("#spcReverseInventory").val() == "" ){
						alert("请输入库存保留量");
						$("#spcReverseInventory").focus();
						return false;
					}
					if ($("#spcMaxCountPerOrder").val() == "" ){
						alert("请输入订单,单笔最大可销售数");
						$("#spcMaxCountPerOrder").focus();
						return false;
					}
					if ($("#spcMinCountPerOrder").val() == "" ){
						alert("请输入订单,单笔最小可销售数");
						$("#spcMinCountPerOrder").focus();
						return false;
					}
					if(parseInt($("#spcMaxCountPerOrder").val())<parseInt($("#spcMinCountPerOrder").val())){
						alert("单笔最大可销售数不可小於 单笔最小可销售数");
						$("#spcMaxCountPerOrder").focus();
						return false;
					}
					if(parseInt($("#spcMinCountPerOrder").val())<=parseInt(0)){
						alert("单笔最小可销售数要大於0");
						item.focus();
						throw item;
					}
					if ($("#spcSafeInventory").val() == "" ){
						alert("请输入安全库存量");
						$("#spcSafeInventory").focus();
						return false;
					}
					if ($("#spcHotSaleInventory").val() == "" ){
						alert("热销 数量要大於等于0");
						$("#spcHotSaleInventory").focus();
						return false;
					}					
					if ($("#showSeq").val() == "" ){
						alert("请输入排序【0～99】");
						$("#showSeq").focus();
						return false;
					}
				}

				//若该商品已被列入搭售时,规格只能有一个,不可以再新增规格
				try{
					if(parseInt($("#specListsize").val())>0){
						var productId=$("#productId").val();
						$.getJSON("<%=ServerValue.getMidContextPath()%>/product/findAddonProdSpec.do?type=json", 
								{productId : productId}
							,function(jsonAddonProdSpec){
								if(jsonAddonProdSpec=='canOnlyOneSpec'){
									alert('该商品已被列入搭售时,规格只能有一个,不可以再新增规格');
									throw "canOnlyOneSpec";
								}
						});
					}
				}catch(e){
					return false;
				}
				$("#specForm").submit();
			});

	});
	/*
	function sumInv(){
		var sumInvs = 0;
		$("input[name='spcHotSaleInventory']").each(function(index){
			sumInvs = sumInvs + parseInt($(this).val(),10);
		});
		$("td[id*='totalInv_']").each(function(){
			$(this).text(sumInvs + parseInt($("#totalInventory").val(),10));
		});
	}*/
--></script>			
<h1>商品管理 - 商品规格<br/>
	<div class="f-link">
		<a href="<%=ServerValue.getMidContextPath()%>/product/list.do">回商品列表页</a>
	</div>
</h1>
<s:actionerror />
<s:actionmessage />
<s:fielderror />
<br/>
<c-rt:if test="${not empty product.productId}">
	<jsp:include page="commonMenu.jsp"/>
</c-rt:if>
<div class="data-form">
	<s:form action="spec.do" method="post">
		<b>狀態：</b>
	  <s:select name="specStatusParam" list="specStatusMenu.options" listKey="key"  listValue="value.name"/>
	  <input type="hidden" name="productId" value="${product.productId}"/>		
		<input type="button" value="搜寻" onclick="this.form.submit();" />
	</s:form>
</div>
<s:form action="saveSpec.do" method="post" id="specForm">
	<table class="tb-data" width="100%">
		<thead>
		 <tr>
				<th>商品名称<br/>（商品编号）</th>
				<td colspan="5">${product.productName}(${productId})</td>
				<td colspan="7">缺货登记数：${stockQty}&nbsp;&nbsp;
					<fmt-rt:formatDate value="${product.arrivalDate}" pattern="yyyy-MM-dd" var="stockDate"/>
					预计到货：<input type="text" name="arrivalDate" id="arrivalDate" size="10" maxlength="10" value="${stockDate}"/>
					<input type="button" id="submitStock" value="确定"/>
				</td>
		 </tr>		
	   <tr>
	       <th>*规格名称<BR />(例如:蓝-XL,..)<BR />长度:25</th>
	       <th>规格料号<BR />(供应商自行管理之编号)<BR />长度:20</th>
	       <th>规格条码<BR />(国际标准bar code)<BR />长度:20</th>
	       <th>*库存保留量<BR />(供退换货使用)</th>
	       <th>*订单单笔<BR />最大可销售数量</th>
	       <th>*订单单笔<BR />最小可销售数量</th>
	       <th>*安全库存量</th>
	       <th>可售数</th>
				 <th>已售数</th>
				 <th>取消数</th>
				 <th>热销数</th>
	       <th>*排序<br />0～99</th>
	       <th>*规格状态</th>
	       <th>删除</th>
	       <th>销售中库存查询 </th>
	   </tr>
		</thead>
	  <tbody>
			<c-rt:forEach items="${specifications}" var="specification" varStatus="rows_index">        
				<tr class="${rows_index.count % 2 == 0 ? 'bg-yl' : ''}" id="tdDesc_${rows_index.count}" title="建立者:${specification.createUser} 建立日期:${specification.createDate}<br/>修改者:${specification.modifyUser} 修改日期:${specification.modifyDate}">	
					<td>
						<input type="hidden" name="spcId" value="${specification.specificationId}" />
						<input type="text" name="spcName" id="spcName_${rows_index.count}" size="15" maxlength="25" value="${specification.specificationName}" />
					</td>
					<td><input type="text" name="spcCode" id="spcCode" size="10" maxlength="20" value="${specification.specCode}" /></td>
					<td><input type="text" name="spcBarCode" id="spcBarCode" size="10" maxlength="20" value="${specification.barCode}" /></td>
					<td><input type="text" name="spcReverseInventory" maxlength="8" id="spcReverseInventory_${rows_index.count}" size="5" value="${specification.reverseInventory}" /></td>
					<td><input type="text" name="spcMaxCountPerOrder" maxlength="8" id="spcMaxCountPerOrder_${rows_index.count}" size="5" value="${specification.maxCountPerOrder}" /></td>
					<td><input type="text" name="spcMinCountPerOrder" maxlength="8" id="spcMinCountPerOrder_${rows_index.count}" size="5" value="${specification.minCountPerOrder}" /></td>
					<td><input type="text" name="spcSafeInventory" maxlength="8" id="spcSafeInventory_${rows_index.count}" size="5" value="${specification.safeInventory}" /></td>
					<td>${(specificationinventoryMap[specification.specificationId][0] - specificationinventoryMap[specification.specificationId][1] + specificationinventoryMap[specification.specificationId][2])<=0?'0':(specificationinventoryMap[specification.specificationId][0] - specificationinventoryMap[specification.specificationId][1] + specificationinventoryMap[specification.specificationId][2])}</td>
					<td>${specificationinventoryMap[specification.specificationId][1]}</td>
					<td>${specificationinventoryMap[specification.specificationId][2]}</td>
					<td><input type="text" name="spcHotSaleInventory" maxlength="8" id="spcHotSaleInventory_${rows_index.count}" size="5" value="${specification.hotSaleInventory}" /></td>
					<td><input type="text" name="showSpecSeq" maxlength="2" id="showSeq_${rows_index.count}" size="2" value="${empty specification.showSeq?'0':specification.showSeq}" /></td>
     			<td>
           	<select name="specStatus">
           		<c-rt:forEach items="${specSubStatusMenu.options}" var="op">
           			<option value="${op.key}" ${op.key eq specification.specStatus ? "selected=selected" : ""}>${op.value.name}</option>
           		</c-rt:forEach>
           	</select>
					</td>
					<td>
						<input name="spcDelete" type="checkbox" value="${specification.specificationId}" ${specification.isDelete eq "Y" ? "checked='checked'" : "" }/>
					</td>	
					<td>
					<a href="<%=ServerValue.getMidContextPath()%>/product/orderNotShipping.do?specificId=${specification.specificationId}&productId=${productId}" target="_blank">查询</a>
					</td>	
				</tr>
			</c-rt:forEach>       
			<tr class="">
	    	<td>
	      	<input type="hidden" name="spcId" value="" />
					<input type="text" name="spcName" id="spcName" size="15" maxlength="25" value="" />
				</td>
	      <td><input type="text" name="spcCode" id="spcCode" size="10" maxlength="20" value="" /></td>
	      <td><input type="text" name="spcBarCode" id="spcBarCode" size="10" maxlength="20" value="" /></td>
	      <td><input type="text" name="spcReverseInventory" maxlength="8" id="spcReverseInventory" size="5" value="" /></td>
	      <td><input type="text" name="spcMaxCountPerOrder" maxlength="8" id="spcMaxCountPerOrder" size="5" value="" /></td>
	      <td><input type="text" name="spcMinCountPerOrder" maxlength="8" id="spcMinCountPerOrder" size="5" value="" /></td>
	      <td><input type="text" name="spcSafeInventory" maxlength="8" id="spcSafeInventory" size="5" value="" /></td>
	      <td>0</td>
				<td>0</td>
				<td>0</td>
				<td><input type="text" name="spcHotSaleInventory" maxlength="8" id="spcHotSaleInventory" size="5" value="0" /></td>
	      <td><input type="text" name="showSpecSeq" maxlength="2" id="showSeq" size="2" value="0" /></td>
   			<td>
         	<select name="specStatus">
         		<c-rt:forEach items="${specSubStatusMenu.options}" var="op">
         			<option value="${op.key}">${op.value.name}</option>
         		</c-rt:forEach>
         	</select>
				</td>	      
	      <td></td>
	    </tr>                        
	  </tbody>			
		<tr class="">
			<td>
				<input type="hidden" id="totalInventory" value="${totalInventory}"/>
				<input type="hidden" id="specListsize" value="${fn:length(specifications)}"/>
				<input type="hidden" id="productId" name="productId" value="${productId}"/>
				<input type="button" id="submit_button" value="储存商品规格"/>
			</td>
		</tr>
	</table>
 </s:form>